//
//  CDEditControlBar.swift
//  PDF
//
//  Created by Kellv on 2024/4/19.
//

import UIKit

class CDEditControlBar: UIView {

    var leftActionHandler: ((_ isCancel: Bool) -> Void)?
    var rightActionHandler: ((_ isDone: Bool) -> Void)?

    var titelLabel:UILabel!
    private let leftBtn = UIButton(type: .custom)
    private let rightBtn = UIButton(type: .custom)

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        leftBtn.frame = CGRect(x: 16, y: 16, width: 80, height: 32)
        leftBtn.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 12)
        leftBtn.layer.cornerRadius = 16
        leftBtn.addTarget(self, action: #selector(onCancelAction), for: .touchUpInside)
        self.addSubview(leftBtn)
    

        rightBtn.frame = CGRect(x: frame.width - 16 - 80, y: 16, width: 80, height: 32)
        rightBtn.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 12)
        rightBtn.layer.cornerRadius = 16
        rightBtn.addTarget(self, action: #selector(onDoneAction), for: .touchUpInside)
        self.addSubview(rightBtn)
        
        updateUI(leftBtnTitle: "Cancel", rightBtnTitle: "Done")
        
        titelLabel = UILabel(frame: CGRect(x: frame.midX - 24, y: frame.midY - 16, width: 48, height: 32))
        titelLabel.textColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
        titelLabel.font = UIFont(name: "Helvetica-Bold", size: 14)
        titelLabel.backgroundColor = UIColor(hexStr: "7F89A1")
        titelLabel.textAlignment = .center
        titelLabel.text = "0/0"
        titelLabel.addRadius(corners: .allCorners, size: CGSize(width: 16, height: 16))
        self.addSubview(titelLabel)
        

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func onCancelAction() {
        guard let leftActionHandler = leftActionHandler else {
            return
        }
        leftActionHandler(leftBtn.currentTitle == "Cancel".localize())
    }
    
    @objc func onDoneAction() {
        guard let rightActionHandler = rightActionHandler else {
            return
        }
        rightActionHandler(leftBtn.currentTitle == "Done".localize())
    }
        
    func updateUI(leftBtnTitle:String, rightBtnTitle:String) {
        leftBtn.setTitle(leftBtnTitle.localize(), for: .normal)
        rightBtn.setTitle(rightBtnTitle.localize(), for: .normal)

        if leftBtnTitle == "Cancel".localize() {
            leftBtn.backgroundColor = .cancelColor
            leftBtn.setTitleColor(UIColor(red: 0.498, green: 0.537, blue: 0.631, alpha: 1), for: .normal)

        }else if leftBtnTitle == "A4".localize() {
            leftBtn.backgroundColor = UIColor(red: 0.255, green: 0.443, blue: 1, alpha: 0.15)
            leftBtn.setTitleColor(UIColor(red: 0.255, green: 0.443, blue: 1, alpha: 1), for: .normal)
        }
        
        if rightBtnTitle == "Done".localize() {
            rightBtn.setTitleColor(.white, for: .normal)
            rightBtn.backgroundColor = UIColor(red: 0.255, green: 0.443, blue: 1, alpha: 1)
        } else if rightBtnTitle == "Footer".localize() {
            rightBtn.setTitleColor(UIColor(red: 0.255, green: 0.443, blue: 1, alpha: 1), for: .normal)
            rightBtn.backgroundColor = UIColor(red: 0.255, green: 0.443, blue: 1, alpha: 0.15)
        }
        
    }
}
